package com.example.controller;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static org.jasig.cas.client.util.AbstractCasFilter.CONST_CAS_ASSERTION;

@Controller
@RequestMapping("/test")
public class TestController {
    @ResponseBody
    @RequestMapping("/test1")
    public String test1(HttpSession session){
        Assertion assertion = (Assertion)session.getAttribute(CONST_CAS_ASSERTION);
        AttributePrincipal principal = assertion.getPrincipal();
        String loginName = principal.getName();
        return loginName;
    }

    /**
     * 退出 后自动重定向自定义接口
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println("------------------登出被执行了");
        //它实际上调用的是session对象中的destroy方法
        session.invalidate();
        return "redirect:http://127.0.0.1:8080/cas/logout?service=http://localhost:8088/test/logoutSuccess";
    }

    /**
     * 退出 后自动重定向自定义接口
     */
    @RequestMapping("/autologout")
    public void autologout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println("------------------自动调用");
        //它实际上调用的是session对象中的destroy方法
        session.invalidate();

    }

    /**这里不会成功，因为我在CAS服务端设置了固定的登出重定向地址
     * 退出成功页
     */
    @RequestMapping("/logoutSuccess")
    @ResponseBody
    public String logoutSuccess() {
        return "成功退出！";
    }

}
